package com.vitalc.service.utils;

import java.util.BitSet;

public class BitSetUtils {

    public static BitSet changeIntegerToBitSet(int value) {
        if (value < 0) {
            throw new NumberFormatException(value + " less than zero.");
        }
        BitSet bitSet = new BitSet();
        int index = 0;
        while (value != 0) {
            if ((value & 1) == 1) {
                bitSet.set(index);
            }
            index++;
            value = value >> 1;
        }
        return bitSet;
    }

    public static int changeBitSetToInteger(BitSet bitSet) {
        if (bitSet == null) return 0;
        int value = 0;
        for (int i = 0; i < bitSet.length(); i++) {
            if (bitSet.get(i)) {
                value |= (1 << i);
            }
        }
        return value;
    }

    public static void main(String[] args) {
        System.out.println(changeIntegerToBitSet(41).get(0));
    }
}
